home *** CD-ROM | disk | FTP | other *** search
/ EuroCD 3 / EuroCD 3.iso / Programming / vbcc / tasm.c < prev    next >
C/C++ Source or Header  |  1998-06-24  |  3KB  |  128 lines

  1. /*  Test-language for vbcc. */
  2.  
  3. #include "supp.h"
  4.  
  5. struct Var *fv;
  6.  
  7. struct Typ tint,mfunc;
  8. struct struct_declaration msd; /* initialized to zero */
  9.  
  10. void raus(void)
  11. {
  12.     while(fv){
  13.         struct Var *m=fv->next;
  14.         free(fv);
  15.         fv=m;
  16.     }
  17.     while(first_ic){
  18.         struct IC *m=first_ic->next;
  19.         free(first_ic);
  20.         first_ic=m;
  21.     }
  22.     exit(0);
  23. }
  24. void add_IC(struct IC *new)
  25. {
  26.     new->next=0;
  27.     new->prev=last_ic;
  28.     new->change_cnt=new->use_cnt=0;
  29.     new->line=0;
  30.     new->file=0;
  31.     if(!last_ic){
  32.         first_ic=new;
  33.     }else{
  34.         last_ic->next=new;
  35.     }
  36.     last_ic=new;
  37. }
  38. struct Var *add_var(char *name,struct Typ *t,int sc)
  39. {
  40.     struct Var *v=mymalloc(sizeof(*v));
  41.     v->vtyp=t;
  42.     v->storage_class=sc;
  43.     v->reg=0;
  44.     v->identifier=name;
  45.     v->offset=max_offset;
  46.     if(sc==AUTO) max_offset=zladd(max_offset,sizetab[t->flags&NQ]);
  47.     v->priority=1;
  48.     v->flags=0;
  49.     v->next=fv;
  50.     v->clist=0;
  51.     v->fi=0;
  52.     v->inline_copy=0;
  53.     fv=v;
  54.     return v;
  55. }
  56. struct Var *add_tmp_var(struct Typ *t)
  57. {
  58.     return add_var(empty,t,AUTO);
  59. }
  60. struct Var *get_var(char *name)
  61. {
  62.     struct Var *v;char *buf;
  63.     for(v=fv;v;v=v->next){
  64.         if(!strcmp(name,v->identifier)) return v;
  65.     }
  66.     buf=mymalloc(strlen(name)+1);
  67.     strcpy(buf,name);
  68.     return add_var(buf,&tint,AUTO);
  69. }
  70.  
  71. void read_ics()
  72. {
  73.     char s[400],q1[100],q2[100],z[100],op;
  74.     struct IC *new;
  75.     gets(s);
  76.     while(sscanf(s,"%99s = %99s %c %99s",z,q1,&op,q2)==4){
  77.         new=mymalloc(ICS);
  78.         switch(op){
  79.             case '+': new->code=ADD;break;
  80.             case '*': new->code=MULT;break;
  81.             case '-': new->code=SUB;break;
  82.             case '/': new->code=DIV;break;
  83.             default: return;
  84.         }
  85.         new->typf=INT;
  86.         new->q1.flags=new->q2.flags=new->z.flags=VAR;
  87.         new->q1.am=new->q2.am=new->z.am=0;
  88.         new->q1.val.vlong=l2zl(0L);
  89.         new->q2.val.vlong=l2zl(0L);
  90.         new->z.val.vlong=l2zl(0L);
  91.         new->q1.v=get_var(q1);
  92.         new->q2.v=get_var(q2);
  93.         new->z.v=get_var(z);
  94.         add_IC(new);
  95.         gets(s);
  96.     }
  97. }
  98. void error(int n,...)
  99. {
  100.     printf("error %d\n",n);
  101.     raus();
  102. }
  103. void savescratch()
  104. {}
  105.  
  106. main()
  107. {
  108.     struct Var *main;
  109.     max_offset=l2zl(0L);
  110.     if(!init_cg()) raus();
  111.     tint.flags=INT;
  112.     tint.next=0;
  113.     mfunc.flags=FUNKT;
  114.     mfunc.next=∭
  115.     mfunc.exact=&msd;
  116.     main=add_var("main",&mfunc,EXTERN);
  117.     read_ics();
  118.     printf("optflags: ");
  119.     scanf("%ld",&optflags);
  120.     pric(stdout,first_ic);
  121.     vl1=vl3=0;
  122.     vl2=fv;
  123.     optimize(optflags,main);
  124.     pric(stdout,first_ic);
  125.     gen_code(stdout,first_ic,main,max_offset);
  126.     raus();
  127. }
  128.